/*     */ package com.zimbra.cs.dav.service.method;
/*     */ 
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.dav.DavContext;
/*     */ import com.zimbra.cs.dav.DavElements;
/*     */ import com.zimbra.cs.dav.DavException;
/*     */ import com.zimbra.cs.dav.DavProtocol;
/*     */ import com.zimbra.cs.dav.property.ResourceProperty;
/*     */ import com.zimbra.cs.dav.resource.DavResource;
/*     */ import com.zimbra.cs.dav.resource.UrlNamespace;
/*     */ import com.zimbra.cs.dav.service.DavResponse;
/*     */ import com.zimbra.cs.dav.service.DavResponse.PropStat;
/*     */ import java.io.UnsupportedEncodingException;
/*     */ import java.net.URLDecoder;
/*     */ import java.util.Iterator;
/*     */ import java.util.Map;
/*     */ import org.dom4j.Document;
/*     */ import org.dom4j.DocumentHelper;
/*     */ import org.dom4j.Element;
/*     */ import org.dom4j.QName;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class ExpandProperty
/*     */   extends Report
/*     */ {
/*     */   public void handle(DavContext ctxt)
/*     */     throws ServiceException, DavException
/*     */   {
/*  60 */     Element query = ctxt.getRequestMessage().getRootElement();
/*  61 */     if (!query.getQName().equals(DavElements.E_EXPAND_PROPERTY)) {
/*  62 */       throw new DavException("msg " + query.getName() + " is not expand-property", 400, null);
/*     */     }
/*     */     
/*  65 */     DavResource rs = ctxt.getRequestedResource();
/*  66 */     ctxt.setDavCompliance(DavProtocol.getComplianceString(rs.getComplianceList()));
/*  67 */     ctxt.setStatus(207);
/*  68 */     Element resp = ctxt.getDavResponse().getTop(DavElements.E_MULTISTATUS).addElement(DavElements.E_RESPONSE);
/*  69 */     expandProperties(ctxt, rs, query, resp);
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   private void expandProperties(DavContext ctxt, DavResource rs, Element elem, Element resp)
/*     */   {
/*  79 */     rs.getProperty(DavElements.E_HREF).toElement(ctxt, resp, false);
/*     */     
/*  81 */     Iterator iter = elem.elementIterator(DavElements.E_PROPERTY);
/*  82 */     DavResponse.PropStat propstat = new DavResponse.PropStat();
/*  83 */     while (iter.hasNext()) {
/*  84 */       Element property = (Element)iter.next();
/*  85 */       Prop p = new Prop(property);
/*  86 */       ResourceProperty rp = rs.getProperty(p.getQName());
/*  87 */       if (rp == null) {
/*  88 */         if (!ctxt.isBrief()) {
/*  89 */           propstat.add(p.getQName(), null, 404);
/*     */         }
/*     */       } else {
/*  92 */         Iterator subProps = property.elementIterator();
/*  93 */         if (subProps.hasNext()) {
/*  94 */           DavResponse.PropStat sub = new DavResponse.PropStat();
/*  95 */           sub.add(rp);
/*  96 */           Element subElem = DocumentHelper.createElement(DavElements.E_RESPONSE);
/*  97 */           sub.toResponse(ctxt, subElem, false);
/*     */           
/*  99 */           Iterator subPropstats = subElem.elementIterator(DavElements.E_PROPSTAT);
/* 100 */           while (subPropstats.hasNext()) {
/* 101 */             Element subPropstat = (Element)subPropstats.next();
/* 102 */             Element status = subPropstat.element(DavElements.E_STATUS);
/* 103 */             if (status.getText().equals(DavResponse.sStatusTextMap.get(Integer.valueOf(200))))
/*     */             {
/* 105 */               Element prop = subPropstat.element(DavElements.E_PROP);
/* 106 */               if (prop != null)
/*     */               {
/* 108 */                 prop = prop.element(p.getQName());
/* 109 */                 if (prop != null)
/*     */                 {
/*     */ 
/* 112 */                   Iterator hrefs = prop.elementIterator(DavElements.E_HREF);
/* 113 */                   while (hrefs.hasNext()) {
/* 114 */                     Element href = (Element)hrefs.next();
/* 115 */                     String url = href.getText();
/* 116 */                     if (url != null)
/*     */                     {
/*     */                       try {
/* 119 */                         url = URLDecoder.decode(url, "UTF-8");
/*     */                       } catch (UnsupportedEncodingException e) {
/* 121 */                         ZimbraLog.dav.warn("can't decode url %s", url, e);
/*     */                       }
/*     */                       try {
/* 124 */                         DavResource target = UrlNamespace.getResourceAtUrl(ctxt, url);
/* 125 */                         Element targetElem = DocumentHelper.createElement(DavElements.E_RESPONSE);
/* 126 */                         expandProperties(ctxt, target, property, targetElem);
/* 127 */                         propstat.add(rp.getName(), targetElem);
/*     */                       } catch (DavException e) {
/* 129 */                         ZimbraLog.dav.warn("can't find resource for " + url, e);
/*     */                       }
/*     */                     }
/*     */                   }
/*     */                 }
/* 134 */               } } } } else { propstat.add(rp);
/*     */         }
/*     */       }
/*     */     }
/* 138 */     propstat.toResponse(ctxt, resp, false);
/*     */   }
/*     */   
/*     */   private static class Prop {
/*     */     private String mName;
/*     */     private String mNamespace;
/*     */     private QName mQName;
/*     */     
/*     */     public Prop(Element propElement) {
/* 147 */       this.mName = propElement.attributeValue("name");
/* 148 */       this.mNamespace = propElement.attributeValue("namespace", "DAV:");
/* 149 */       this.mQName = QName.get(this.mName, this.mNamespace);
/*     */     }
/*     */     
/* 152 */     public QName getQName() { return this.mQName; }
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/dav/service/method/ExpandProperty.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */